package com.itlr.day04;

import java.util.ArrayList;
import java.util.Scanner;

public class main01 {
    public static void main(String[] args) {
        /* 模拟登陆，编写用户类，测试类。
         1.给集合中存储5个用户对象。
         2，从控制台输入用户名和密码，和集合中的对象信息进行比较，
         相同表示成功，不同表示失败。
         3.有三次输入机会。*/

        //1.给集合中存储5个用户对象
        ArrayList<main01.User> users = new ArrayList<>();
        users.add(new main01.User("admin", "123456"));
        users.add(new main01.User("user1", "1234567"));
        users.add(new main01.User("user2", "12345678"));
        users.add(new main01.User("user3", "123456789"));
        users.add(new main01.User("user4", "1234567890"));

        Scanner scanner = new Scanner(System.in);
        int attempts = 3;

        // 3.有三次输入机会
        while (attempts > 0) {
            System.out.println("请输入用户名:");
            String inputUsername = scanner.nextLine();
            System.out.println("请输入密码:");
            String inputPassword = scanner.nextLine();

            // 2.从控制台输入用户名和密码，和集合中的对象信息进行比较
            boolean loginSuccess = false;
            for (main01.User user : users) {
                if (user.getUsername().equals(inputUsername) &&
                    user.getPassword().equals(inputPassword)) {
                    loginSuccess = true;
                    break;
                }
            }

            if (loginSuccess) {
                System.out.println("登录成功!");
                break;
            } else {
                attempts--;
                if (attempts > 0) {
                    System.out.println("登录失败，还有" + attempts + "次机会");
                } else {
                    System.out.println("登录失败，机会已用完");
                }
            }
        }
    }

    static class User {
        private String username;
        private String password;

        public User(String username, String password) {
            this.username = username;
            this.password = password;
        }

        // getter方法
        public String getUsername() {
            return username;
        }

        public String getPassword() {
            return password;
        }
    }
}
